/**
 * @Author: 十二少 1484744996@qq.com
 * @Date: 2023-03-07 16:53:04
 * @LastEditors: 九璃怀特 1599130621@qq.com
 * @LastEditTime: 2023-12-29 10:21:06
 * @FilePath: \zxi-surface-manual\electron\preload\useElectronAPI.ts
 * @Description: 
 */
import { contextBridge, ipcRenderer } from 'electron'
/** 
 * @description: 设置window.electronAPI
 * @return {*}
 */
export function useElectronAPI () {
  contextBridge.exposeInMainWorld('electronAPI', {
    readConfig: () => ipcRenderer.invoke(PreloadName.readConfig),
    update:(response:boolean)=>ipcRenderer.invoke(PreloadName.update,response),
    setFullScreen: () => ipcRenderer.invoke('setFullScreen'),
    getFullScreen: () => ipcRenderer.invoke('getFullScreen'),
    checkUpdate:()=>ipcRenderer.invoke(PreloadName.checkUpdate),
    controlApp: (status: 'min' | 'max' | 'close') => ipcRenderer.send(PreloadName.controlApp, status),
    toggleDevTools: () => ipcRenderer.send(PreloadName.toggleDevTools),
    cleanCache:()=>ipcRenderer.send(PreloadName.cleanCache),
    restart:()=>ipcRenderer.send(PreloadName.restart),
    updateAvailable:(val)=>ipcRenderer.on(PreloadName.updateAvailable,val),
    downloadProgress:(progress)=>ipcRenderer.on(PreloadName.downloadProgress,progress),
    info:(msg)=>ipcRenderer.on('info',msg),
    downloaded:(msg)=>ipcRenderer.on('downloaded',msg),
    message:(msg)=>ipcRenderer.on('message',msg)
  })
}

export class PreloadName {
  /** 
   * @description: 传递配置文件
   */  
  static readConfig = 'readConfig'
  /** 
   * @description: 控制app的状态，最大化、最小化、退出
   */  
  static controlApp = 'controlApp'
  /** 
   * @description: 打开调试工具
   */
  static toggleDevTools = 'toggleDevTools'
  /** 
   * @description: 清除缓存
   */
  static cleanCache = 'cleanCache'
  /**
  * @description: 重启应用
  */
  static restart = 'restart'
  /** 
   * @description: 告诉主线程是否要更新
   */
  static update = 'update'
  /** 
   * @description: 检测应用更新 主线程到渲染器
   */
  static updateAvailable = 'updateAvailable'
  /** 
   * @description: 检测应用更新 渲染器到主线程
   */
  static checkUpdate = "checkUpdate"
  /** 
   * @description: 更新包下载进度
   */
  static downloadProgress = "downloadProgress"
  /** 
   * @description: log
   */
  static info = 'info'
  /** 
   * @description: 更新下载完成回调
   */
  static downloaded = 'downloaded'
}